package com.ihr360.payroll.service.salaryprofile.impl;

import com.google.common.collect.Lists;
import com.ihr360.authority.sharing.annonation.AccessControl;
import com.ihr360.commons.context.session.Ihr360SessionContextHolder;
import com.ihr360.commons.lang.CollectionUtils;
import com.ihr360.payroll.dao.SalaryProfileFieldRepository;
import com.ihr360.payroll.dao.SystemSalaryFieldRepository;
import com.ihr360.payroll.model.dto.salary.meta.system.SystemSalaryField;
import com.ihr360.payroll.model.dto.staff.SalaryProfileField;
import com.ihr360.payroll.service.salaryprofile.SalaryProfileFieldService;
import com.ihr360.rest.reposity.SimpleRestReposity;
import com.ihr360.rest.service.AbstractSimpleRestServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
@AccessControl(disableAll = true)
public class SalaryProfileFieldServiceImpl extends AbstractSimpleRestServiceImpl<SalaryProfileField, Long> implements SalaryProfileFieldService {

    public static final List<String> COMPANY_INIT_SALARY_PROFILE_CODES = Arrays.asList("basesalary");

    @Autowired
    private SalaryProfileFieldRepository salaryProfileFieldRepository;

    @Autowired
    private SystemSalaryFieldRepository systemSalaryFieldRepository;

    @Override
    public SimpleRestReposity<SalaryProfileField, Long> getSimpleRestReposity() {
        return salaryProfileFieldRepository;
    }

    @Override
    public List<SalaryProfileField> getCompanyInitProfileFields() {
        List<SystemSalaryField> systemSalaryFields =  systemSalaryFieldRepository.findByCodeIn(COMPANY_INIT_SALARY_PROFILE_CODES);
        if (CollectionUtils.isEmpty(systemSalaryFields)) {
            return Lists.newArrayList();
        }
        List<SalaryProfileField> salaryProfileFields = Lists.newArrayListWithCapacity(systemSalaryFields.size());
        systemSalaryFields.forEach(systemSalaryField -> {
            SalaryProfileField salaryProfileField = new SalaryProfileField();
            BeanUtils.copyProperties(systemSalaryField,salaryProfileField,"id","indexno");
            salaryProfileFields.add(salaryProfileField);
        });
        return salaryProfileFields;
    }

    @Override
    public void deleteOne(Long id) {
        String companyId = Ihr360SessionContextHolder.getCompanyId();
        salaryProfileFieldRepository.deleteByCompanyIdAndId(companyId, id);
    }

    @Override
    public void batchDelete(List<Long> ids) {
        String companyId = Ihr360SessionContextHolder.getCompanyId();
        salaryProfileFieldRepository.deleteByCompanyIdAndIdIn(companyId, ids);
    }

}
